﻿using System.Data.Objects;
using System.Runtime.Remoting.Messaging;
using WxxMail.Domain;
using System.Data.Entity;


namespace WxxMail.DAO
{
       public class ObjectContextFactory
        {
            public static System.Data.Objects.ObjectContext GetCurrentObjectContext()
            {
                //从CallContext数据槽中获取EF上下文
                ObjectContext objectContext = CallContext.GetData(typeof(ObjectContextFactory).FullName) as ObjectContext;
                if (objectContext == null)
                {
                    //如果CallContext数据槽中没有EF上下文，则创建EF上下文，并保存到CallContext数据槽中
                    objectContext = new ArticleEntities();//当数据库替换为MySql等，只要在次出EF更换上下文即可。
                    CallContext.SetData(typeof(ObjectContextFactory).FullName, objectContext);
                }
                return objectContext;
            }
        }
    }
